﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Radiation___IM.Data;
using Radiation.Common;

namespace Radiation___IM.Users
{
    public class User
    {
        private string m_nickname;
        private Int64 m_id;
        private string m_password;
        private List<string> m_friends;
        private string m_permission_group;
        private UserPermissionGroup m_specific_permissions;

        public string Nickname
        {
            get
            {
                return this.m_nickname;
            }
            set
            {
                this.m_nickname = value;
            }
        }

        public string PermissionGroup
        {
            get
            {
                return this.m_permission_group;
            }
            set
            {
                this.m_permission_group = value;
            }
        }

        public string Password
        {
            get
            {
                return this.m_password;
            }
            set
            {
                this.m_password = value;
            }
        }

        public List<string> Friends
        {
            get
            {
                return this.m_friends;
            }
        }

        public Int64 ID
        {
            get
            {
                return m_id;
            }
        }

        public File getFile()
        {
            File file = new File("users\\" + this.m_nickname + ".dat");

            file.addLine("m_id", Convert.ToString(this.m_id));
            file.addLine("m_nickname", this.m_nickname);
            file.addLine("m_password", this.m_password);
            file.addLine("m_friends", serializeFriends());
            file.addLine("m_permission_group", this.m_permission_group);
            file.addLine("m_specific_permissions", this.m_specific_permissions.serialize());

            return file;
        }

        public void setFile(File file)
        {
            try
            {
                this.m_id = Convert.ToInt64(file["m_id"]);
                this.m_nickname = file["m_nickname"];
                this.m_password = file["m_password"];
                this.unserializeFriends(file["m_friends"]);
                this.m_permission_group = file["m_permission_group"];
                this.m_specific_permissions.unserialize(file["m_specific_permissions"]);

                if (this.m_nickname == null || this.m_password == null)
                {
                    Logger.Instance["error"].add("Users.User.setFile : one value is null");
                }
            }
            catch (Exception e)
            {
                Logger.Instance["error"].add("Users.User.setFile : error occured : " + e.ToString());
            }
        }

        public User()
        {
            m_nickname = "";
            m_password = "";
            m_id = 0;
            m_friends = new List<string>();
            m_permission_group = "";
            m_specific_permissions = new UserPermissionGroup();
        }

        public void setId(Int64 id)
        {
            this.m_id = id;
        }

        public bool isAllowedTo(string permission)
        {
            return (isAllowedByGroup(permission) || m_specific_permissions[permission] || m_specific_permissions["full"]);
        }

        /// <summary>
        /// Will check if the permission group exists & if the permission is granted
        /// </summary>
        /// <param name="permission">Permission name</param>
        /// <returns>True if explicitly allowed, otherwise false</returns>
        private bool isAllowedByGroup(string permission)
        {
            if (m_permission_group.Length > 0 &&
                PermissionGroupManager.Instance[m_permission_group] != null &&
                (PermissionGroupManager.Instance[m_permission_group][permission] || PermissionGroupManager.Instance[m_permission_group]["full"]))
                return true;

            return false;
        }

        public string serializeFriends()
        {
            StringBuilder final = new StringBuilder();

            foreach (string friend in this.m_friends)
                final.Append(friend + ";");

            if (this.m_friends.Count > 0)
                return final.ToString().Substring(0, final.ToString().Length - 1);
            else
                return "";
        }

        private void unserializeFriends(string serialized)
        {
            this.m_friends.Clear();

            if (serialized.Length > 0)
            {
                string[] friends = serialized.Split(new char[] { ';' });
                this.m_friends.AddRange(friends);
            }
        }
    }
}
